// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <vector>

#include <pollux/common/base/exceptions.h>
#include <pollux/vector/complex_vector.h>

#include <melon/likely.h>

namespace kumo::pollux {
    /// Abstract class defining the interface for a stream of values to read in
    /// batch by UnorderedStreamReader.
    class BatchStream {
    public:
        virtual ~BatchStream() = default;

        /// Returns the next batch from the stream. The function returns true with
        /// read data in 'batch', otherwise returns false if it reaches to the end of
        /// the stream.
        virtual bool nextBatch(RowVectorPtr &batch) = 0;
    };

    /// Implements a unordered reader to read from a group of streams with one at a
    /// time in batch. The reader owns the streams. At each call of nextBatch(), it
    /// returns the next batch from the current reading stream, and the reader will
    /// switch to the next stream internally if the current stream reaches to the
    /// end.
    ///
    /// NOTE: this object is not thread safe.
    template<typename BatchStream>
    class UnorderedStreamReader {
    public:
        explicit UnorderedStreamReader(
            std::vector<std::unique_ptr<BatchStream> > streams)
            : currentStream(0), streams_(std::move(streams)) {
            static_assert(std::is_base_of_v<BatchStream, BatchStream>);
        }

        /// Returns the next batch from the current reading stream. The function will
        /// switch to the next stream if the current stream reaches to the end. The
        /// function returns true with read data in 'batch', otherwise returns
        /// false if all the streams have been read out.
        bool nextBatch(RowVectorPtr &batch) {
            while (MELON_LIKELY(currentStream < streams_.size())) {
                if (MELON_LIKELY(streams_[currentStream]->nextBatch(batch))) {
                    return true;
                }
                ++currentStream;
            }
            return false;
        }

    private:
        // Points to the current reading stream in 'streams_'.
        vector_size_t currentStream{0};
        // A list of streams to read in batch sequentially.
        std::vector<std::unique_ptr<BatchStream> > streams_;
    };
} // namespace kumo::pollux
